<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
            text-decoration: none;
        }
        .box {
            width: 350px;
            height: 400px;
            background: yellowgreen;
            margin: 0 auto;
            border: 1px solid pink;
            text-align: center;
        }
        .box1 {
            margin-top: 100px;
        }
        .box1 > button {
            width: 167px;
            background: orange;
            margin-left: 62px;
            cursor: pointer;
        }
        span {
            display: flex;
            padding-top: 40px;
            padding-left: 225px;
            cursor: pointer;
        }
        h1 {
            height: 60px;
            line-height: 60px;
            background: yellow;
        }
    </style>
</head>
<body>
    <div class="box">
        <h1>登录页</h1>
        <div class="box1">
            会员名：<input type="text" name="username" id="txt"><br>
            密码：<input type="password" name="password" id="pass"><br>
            <button>登录</button>
            <span><a href="javascript:;">免费注册</a></span>
        </div>
    </div>
</body>
</html>
<script src="./ajax.js"></script>
<script>
    function $(tag) {
        return document.querySelector(tag);
    }

    $('button').onclick = function () {
        $ajax({
            type: 'POST',
            url: 'regest.php',
            data: `username=${$('#txt').value}&password=${$('#pass').value}`,
            callback(msg) {
                if(msg == "1") {
                    document.cookie =  `username=${$('#txt').value}`;
                    alert('注册成功^_^')
                    setTimeout(() => {
                        location.href = 'login.html';
                    }, 500);
                } else if(msg == "0") {
                    alert('注册失败！')
                }
            }
        })
    }
</script>